package Nonogram;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

/**
 * PuzzleManager
 * @author Rachel Wedewer, Mollie Burkle, Kristen Nielsen
 * Date Modified: 4/30/2014
 * 
 * This is one of the four main components of Nonogram. It handles obtaining
 * the path to the file whether the file is located in the resource folder or elsewhere on 
 * the device. This is the first of the four components executed and the first time ran will return
 * the path of the file located in the resource folder. 
 * 
 * Then if a user clicks the select puzzle or import button from the GUI it will call PuzzleManger methods: getSelectedPuzzle() and
 * getImportPuzzle(). getSelectedPuzzle() acts the same when the system is started up, but the user can cancel selecting a new puzzle.
 * getImportPuzzle() allows the user to select a file path outside the scope of the resource folder.  (THIS MAY CAUSE ISSUES).
 */
public class PuzzleManager {

	
	private String[][] puzzleMan;
	
	
	public PuzzleManager(){
		puzzleMan = new String[6][]; 
		
		//5x5
		puzzleMan[0] = new String[] {"Dog.txt", 
									"Heart.txt",
									"House.txt",
									"Pawn.txt",
									"Turtle.txt",
					      	        "Giraffe.txt",
					                "Cat.txt"
									};
		
		//10x10
		puzzleMan[1] = new String[] {"Cat.txt", 
									"Heart.txt",
									"House.txt",
									"Question.txt",
									"Elephant.txt",
									"Fishy.txt",
									"HumBird.txt",
									"Chicken.txt"};
		//15x15
		puzzleMan[2] = new String[] {"Smiley.txt",
									"Frame.txt",
									"Apple.txt",
									"Fencing.txt",
									"Bunny.txt",
									"Cow.txt",
									"Giraffe.txt"									
									};
		
//		//20x20
		puzzleMan[3] = new String[] {"Puppy.txt",
									 "Face.txt",
									 "Android.txt"};
//		
		//25x25
		puzzleMan[4] = new String[] {"Maple Leaf.txt",
									"Cobra.txt",
									"Moon.txt"};

		//30x30
		puzzleMan[5] = new String[] {"Snail.txt"};
		
	}
	
	
	public String getFileName(){
		int size = getSizeDialog();
	    if (size == -1){
	    	  //User hit exit
	    	  return "NOPE";
	     } 
	      
      
	      int puzzle = getSelectedPuzzle(size);
	      if (puzzle == -1){
	    	  //User hit exit
	    	  return "NOPE";
	      } 
	      
	      return selectedPuzzleChoosen(size, puzzle);
	}

	public int getSizeDialog(){
		int messageType = JOptionPane.QUESTION_MESSAGE;
		String[] options = {"5x5", "10x10", "15x15", "20x20","25x25", "30x30"};
		  
		
		int size = JOptionPane.showOptionDialog(null, 
 	           "Pick puzzle size", 
 	           "Option Dialog Box", 0, messageType, 
 	           null, options, "PHP");
		
		return size;
	}
	
	public int getSelectedPuzzle(int size){
		int messageType = JOptionPane.QUESTION_MESSAGE;
							
		int puzzle = JOptionPane.showOptionDialog(null, 
	 	         "Select Puzzle", 
	 	         "Option Dialog Box", 0, messageType, 
			     null, puzzleMan[size], "PHP");	
		return puzzle;		
	}
	
	//Gets the path to a file in res and doesn't allow the user exit 
	public String getFirstPuzzle(){

	      int size = -1;
	      int puzzle = -1;
	      
	      while(true){
	      size = getSizeDialog();
		      if (size != -1){
		    	  //User hit exit
		    	  break;
		      }
	      }
	      
	      while(true){
	      puzzle = getSelectedPuzzle(size);
		      if (puzzle != -1){
		    	  //User hit exit
		    	  break;
		      	} 
	      }
	      
	      return selectedPuzzleChoosen(size, puzzle);
	}
	
	public String[][] getPuzzleMan() {
		return puzzleMan;
	} 
	
	//Gets the path to a file in res folder
	private String selectedPuzzleChoosen(int size, int puzzle){
	      if (size == 0){
	    	  return ("res/5x5/" + puzzleMan[size][puzzle]); 
	      }
	      
	      else if (size == 1){
	    	  return ("res/10x10/" + puzzleMan[size][puzzle]); 
	      }
	      
	      else if (size == 2){
	    	  return ("res/15x15/" + puzzleMan[size][puzzle]); 
	      }
	      else if (size == 3){
	    	  return ("res/20x20/" + puzzleMan[size][puzzle]); 
	      }
	      else if (size == 4){
	    	  return ("res/25x25/" + puzzleMan[size][puzzle]); 
	      }
	      else if (size == 5){
	    	  return ("res/30x30/" + puzzleMan[size][puzzle]); 
	      }
	      else {
	    	  return "NOPE"; 
	      }
	}
	
	//Gets the path to a file from device
	public String getImportPuzzle() {
		JFileChooser chooser = new JFileChooser();
		chooser.setCurrentDirectory(new java.io.File("."));
		chooser.setDialogTitle("choosertitle");
		chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
		chooser.setAcceptAllFileFilterUsed(false);
	
		if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
	    return (chooser.getSelectedFile().toString());
		
	  } 
		else {
			return "NOPE";
	  }
	}
	
	
}
